package Test;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class MouseEventTest {
    private static void Mouse() {

        JFrame frame = new JFrame("鼠标事件");
        frame.setLayout(new FlowLayout());
        frame.setSize(300, 200);
        frame.setLocation(300, 200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JButton button = new JButton("点击");
        frame.add(button);
        button.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON1) {
                    System.out.println("左击");
                }
                if (e.getButton() == MouseEvent.BUTTON3) {
                    System.out.println("右击");
                }
                if (e.getButton()==MouseEvent.BUTTON2){
                    System.out.println("中击");
                }
            }

            public void mousePressed(MouseEvent e) {
                System.out.println("长按");

            }
            public void mouseReleased(MouseEvent e) {
                System.out.println("松开");
            }
            public void mouseEntered(MouseEvent e) {
                System.out.println("进入");
            }
            public void mouseExited(MouseEvent e) {
                System.out.println("出去");
            }
        });
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(MouseEventTest::Mouse);
    }

}
